package com.zhouyu.nettysvr.config;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @description: protobuf
 * @author: zhouyu
 * @create: 2020-05-08 12:40
 */
public class TcpServerConfig {
    //    @Autowired
//    private MyLengthFieldBasedFrameDecoder lengthFieldBasedFrameDecoder;

    //    @Autowired
//    private NettyServerHandler serverHandler;
    private static final int serverPort=9091;
    private static NioEventLoopGroup bossGroup = new NioEventLoopGroup();
    private static NioEventLoopGroup workGroup = new NioEventLoopGroup();
    private static ServerBootstrap serverBootstrap = new ServerBootstrap();
    ChannelFuture channelFuture=null;
    public void run() {
        try {
            serverBootstrap.group(bossGroup,workGroup);
            serverBootstrap.channel(NioServerSocketChannel.class);
            channelFuture= serverBootstrap.bind(serverPort).sync();
            if (channelFuture.isSuccess()) {
                System.out.println("服务端启动成功");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            channelFuture.channel().closeFuture();
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }
}
